package com.hzvdvmc.autosale.socket;

import android.text.TextUtils;

import com.hzvdvmc.autosale.sale.bean.PaySuccessBean;
import com.hzvdvmc.autosale.util.GsonUtils;
import com.hzvdvmc.autosale.util.SPUtils;

/**
 * 售货缓存管理
 * 说明：当正在出货时，对新的下推出货不响应。
 * 数据保存在sp中
 */
public class SaleCacheManager {

    private static String CACHE_KEY = "cache_key";
    public static final int order_state_unout = 1;//未出货
    public static final int order_state_outing = 2;//正在出货
    public static final int order_state_out_success = 3;//出货成功
    public static final int order_state_report_success = 4;//上报成功

    public static void clearCache(){
        SPUtils.getInstance().put(CACHE_KEY,"");
    }

    public static void insertCache(PaySuccessBean bean){
        if (bean == null){
            SPUtils.getInstance().put(CACHE_KEY,"");
        }else {
            String json = GsonUtils.GsonString(bean);
            SPUtils.getInstance().put(CACHE_KEY,json);
        }
    }

    public static PaySuccessBean getCache(){
        PaySuccessBean bean = null;
        String json = SPUtils.getInstance().getString(CACHE_KEY);
        if (!TextUtils.isEmpty(json)){
            bean = GsonUtils.GsonToBean(json, PaySuccessBean.class);
        }
        return bean;
    }

    public static void updateCache(String result,int state){
        PaySuccessBean cache = getCache();
        if (cache != null){
            cache.setOutResult(result);
            cache.setOutState(state);
            insertCache(cache);
        }
    }
}
